package com.example.page;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

public class Page {
	private int pageCount;// 总页数
	private int itemCount;// 总条数
	private int selectPage = 1;// 当前页数
	private int pageSize = 10;// 每页的条数
	private String url;// 基本url
	private List data;// 当前页的数据

	public int getFrom() {
		return 1 + (selectPage - 1) * pageSize;
	}

	public int getTo() {
		if (selectPage == pageCount) {
			return itemCount;
		} else {
			return selectPage * pageSize;
		}
	}

	public boolean isFirst() {
		return selectPage == 1;
	}

	public boolean isLast() {
		return selectPage == pageCount;
	}

	public int getPrevious() {
		return selectPage - 1;
	}

	public int getNext() {
		return selectPage + 1;
	}

	/**
	 * 获取分页切片，用于显示页面数
	 * @return
	 */
	public List<Integer> getSlice() {
		List<Integer> list = new ArrayList<Integer>();
		
		// 显示10个页面的超链接
		int numPerPage = 10;

		int begin = 1;
		int end = numPerPage;
//		分类法
//		if (pageCount <= numPerPage) {
//			begin = 1;
//			end = pageCount;
//		} else {
//			int start = pageCount - numPerPage / 2;
//			int mid = numPerPage / 2 + 1;	// 中间位置，一般用于放当前页面
//			if (selectPage <= mid) {
//				begin = 1;
//				end = numPerPage;
//			} else if (selectPage >= start) {
//				begin = pageCount - (numPerPage - 1);
//				end = pageCount;
//			} else {
//				begin = selectPage - numPerPage / 2;
//				end = begin + numPerPage - 1;
//			}
//		}
		
		// 移动调整法
		begin = selectPage - numPerPage / 2;
		end = begin + numPerPage - 1;
		
		if (begin <= 0) {
			begin = 1;
			end = begin + numPerPage - 1;
			if (end > this.pageCount) {
				end = this.pageCount;
			}
		} else {
			if (end > this.pageCount) {
				end = this.pageCount;
				begin = end - numPerPage + 1;
				if (begin <= 0) {
					begin = 1;
				}
			}
		}

		for (int i = begin; i <= end; i++) {
			list.add(i);
		}

		return list;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getItemCount() {
		return itemCount;
	}

	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}

	public int getSelectPage() {
		return selectPage;
	}

	public void setSelectPage(int selectPage) {
		this.selectPage = selectPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getUrl() {
		if (url == null)
			url = "";
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List getData() {
		return data;
	}

	public void setData(List poList) {
		this.data = poList;
	}

	public String toString() {
		JSONObject json = new JSONObject(this);
		return json.toString();
	}
}
